Skip to content

Distributor: add client-identity ingestion rate limiting - #7947

Open
krunaljain wants to merge 3 commits into
cortexproject:masterfrom
krunaljain:client-identity-ingestion-rate-limiting
Open

krunaljain wants to merge 3 commits into
cortexproject:masterfrom
krunaljain:client-identity-ingestion-rate-limiting

Conversation

@krunaljain

@krunaljain krunaljain commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does:

Adds an optional, experimental per-(tenant, X-Scope-ClientID) ingestion rate limit enforced in the distributor, below the existing tenant-level limit. It implements the write-path proposal merged in #7681 (docs/proposals/client-identity-ingestion-rate-limiting.md, updated in this PR).

  • New per-tenant limits: client_identity_ingestion_rate_limit, client_identity_ingestion_burst_size, client_identity_ingestion_tracked_clients_limit. Defaults of 0 disable the check, so there is no behavior change unless enabled.
  • Counters are gossiped over memberlist across distributor replicas, so the limit is enforced cluster-wide without needing a ring.
  • The client identity is read from a trusted X-Scope-ClientID header set by the same gateway/proxy that sets X-Scope-OrgID; supported on the push and OTLP gRPC paths.
  • Adds a user guide and updates the config reference and schema.

The query-path limit is a separate follow-up.

AI disclosure (per GENAI_POLICY.md): this change was prepared with an AI coding assistant (Claude Code), and reviewed and tested by the author.

Which issue(s) this PR fixes:
N/A

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]
  • docs/configuration/v1-guarantees.md updated if this PR introduces experimental flags

Add an optional per-(tenant, X-Scope-ClientID) ingestion rate limit below the tenant-level limit, as specified in docs/proposals/client-identity-ingestion-rate-limiting.md.

Signed-off-by: Krunal Jain <krunal.bitsy@gmail.com>
@krunaljain
krunaljain requested a review from a team as a code owner November 18, 2026 23:23
// today, exactly as the proposal requires.
if clientLimit := limits.ClientIdentityIngestionRateLimit; clientLimit > 0 {
if clientID := clientid.FromContext(ctx); clientID != "" {
if !d.clientIdentityLimiter.AllowN(now, userID, clientID, totalN) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This 429 still spends tenant tokens. Can we leave that budget available for other writers when a client hits its own limit?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. The client-identity check now runs before the tenant limiter, so a client rejected on its own limit never touches tenant tokens. Added a test where one client is rejected repeatedly and another still gets the full tenant budget.

Comment thread pkg/util/clientlimiter/gossip.go Outdated
}

capacity := int64(limit*g.windowDuration.Seconds()) + int64(burst)
if cs.localCount+cs.peerSum+int64(n) > capacity {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does peerSum expire after a peer goes quiet? Rejected requests never set dirty, so pruning can stop, and applyPeerView skips clients missing from the desc, so a pruned client keeps its old peerSum and stays at 429.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Peers are now stored per replica and summed in AllowN, skipping any counter whose LastUpdateUnixNano is older than retention. A quiet peer stops counting without needing another gossip callback or a dirty flag. applyPeerView also clears peers for tracked clients missing from the desc.

// by pruning entries whose LastUpdateUnixNano has aged out (see GossipLimiter's
// prune loop), not by treating "absent from this particular gossip message" as
// "gone".
func (d *ClientCounterDesc) Merge(mergeable memberlist.Mergeable, _ bool) (memberlist.Mergeable, error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do pruned clients disappear from memberlist? The merge keeps old entries that are missing from the update, so dead replicas keep counting toward peerSum (one rolling restart can block a client for good).

@krunaljain krunaljain Dec 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge never deletes, so I implemented ClientCounterDesc.RemoveTombstones to drop replica counters older than the limit (and empty clients). The read-side retention filter above means correctness doesn't depend on how often memberlist calls it. Added tests for both.

store: memberlist
```

The KV client is created lazily, the first time a tenant actually exercises the feature, so deployments that never opt in start no KV client or watch. On the write path, `X-Scope-ClientID` is read from the HTTP headers (remote write, OTLP over HTTP) or the gRPC metadata (OTLP over gRPC).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The OTLP/HTTP handler doesn't read X-Scope-ClientID. Can we read it there or drop OTLP/HTTP from this list?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. OTLPHandler now injects X-Scope-ClientID the same way the remote write handler does, so the doc stays accurate. Added TestOTLPHandler_InjectsClientID.

…t ID

- Check the client-identity limit before the tenant limiter so a rejected
  client does not spend tenant tokens.
- Track peer counters per replica and ignore ones older than retention at
  admission time, so quiet or dead replicas stop counting. Clear peers for
  clients missing from the gossiped view.
- Implement ClientCounterDesc.RemoveTombstones to drop stale replica
  counters from the merged memberlist state.
- Read X-Scope-ClientID in the OTLP/HTTP handler.

Signed-off-by: Krunal Jain <krunal.bitsy@gmail.com>
Signed-off-by: Krunal Jain <krunal.bitsy@gmail.com>

# Conflicts:
#	pkg/distributor/distributor_test.go

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants